/**************************
* @Summary Itoa函数的实现
* @Author 20009200303 赵传博
* @Date 2022/3/23
*****************************/

#include "ItoaUtil.h"

void ItoaUtil::myItoa(int i, char *b) {
    stack<char> nums;
    int j = 0;
    if(!isNegative(i)){
        while(i){
            char ch = i % 10 + '0';
            //cout << "Test: " << ch << endl;
            nums.push(ch);
            i /= 10;
        }
        while(!nums.empty()){
            b[j++] = nums.top();
            nums.pop();
        }
    }else{
        i = -i;
        while(i){
            char ch = i % 10 + '0';
            //cout << "Test: " << ch << endl;
            nums.push(ch);
            i /= 10;
        }
        while(!nums.empty()){
            b[0] = '-';
            b[++j] = nums.top();
            nums.pop();
        }
    }
}

bool ItoaUtil::isNegative(int i) {
    return i < 0;
}
